/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.administrador.preseleccion;

import pio.action.administrador.*;
import DBMS.DataBaseManager;
import clases.Configuracion;
import clases.Reporte;
import clases.ReportePreseleccion;
import clases.TipoReporte;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author charles
 */
public class GenerarReporte extends DispatchAction {

    public ActionForward page(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("page");
    }

    public ActionForward page2(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("page2");
    }

    public ActionForward make(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager d = DataBaseManager.getInstance();
        ReportePreseleccion reporte = (ReportePreseleccion) form;
        reporte.setTipo(TipoReporte.ASPIRANTE);
        reporte.setNumAutenticacion();
        while (d.existeNumAutenticacion(reporte.getNumAutenticacion())) {
            reporte.setNumAutenticacion();
        }
        d.agregarReporte(reporte);
        reporte.setId(d.buscarID(reporte.getNumAutenticacion()));
        reporte.setPreselectos(d.consultarPreselectos());
        reporte.setNumPreselectos(reporte.getPreselectos().size() / 3);
        reporte.setNumAspirantes(d.numeroAspirantes());
        reporte.setNumNoPreselectos(reporte.getNumAspirantes() - reporte.getNumPreselectos());
        List l = d.consultarPreselectosPorInstitucion();
        List l2 = d.consultarNoPreselectosPorInstitucion();
        int numInst[] = new int[l.size()];
        String nombreInst[] = new String[l.size()];
        Iterator i = l.iterator();
        int n = 0;
        while (i.hasNext()) {
            ArrayList h = (ArrayList) i.next();
            nombreInst[n] = (String) h.get(0);
            numInst[n] = ((Integer) h.get(1)).intValue();
            n++;
        }
        int numInst2[] = new int[l.size()];
        i = l2.iterator();
        n = 0;
        while (i.hasNext()) {
            ArrayList h = (ArrayList) i.next();
            numInst2[n] = ((Integer) h.get(1)).intValue();
            n++;
        }
        reporte.setNumInst(numInst);
        reporte.setNombreInst(nombreInst);
        reporte.setNumInst2(numInst2);
        String path = Configuracion.getReportesPath() + reporte.getTipo() + reporte.getId() + ".pdf";
        try {
            reporte.generar(path);
            reporte.setPath("");
            //path = "/home/charles/Escritorio/servicio comunitario/proy/web/administrador/preseleccion/reportes/aspirante.pdf";
            //f = new File(path);
            //file = new FileOutputStream(f);
            //reporte.generar(file);
            request.getSession().setAttribute("reporte", path);
            request.setAttribute("reporte", path);
            return mapping.findForward("success");
        } catch (Exception e) {
            reporte.setMensaje("Problemas con el servidor.");
            return mapping.findForward("failure");
        }
    }

    public ActionForward make2(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DataBaseManager d = DataBaseManager.getInstance();
        ReportePreseleccion reporte = (ReportePreseleccion) new ReportePreseleccion();
        reporte.setTipo(TipoReporte.ASPIRANTE);
        reporte.setNumAutenticacion();
        while (d.existeNumAutenticacion(reporte.getNumAutenticacion())) {
            reporte.setNumAutenticacion();
        }
        d.agregarReporte(reporte);
        reporte.setId(d.buscarID(reporte.getNumAutenticacion()));
        reporte.setPreselectos(d.consultarPreselectos());
        //ojo no entiendo este entre 3
        reporte.setNumPreselectos(reporte.getPreselectos().size() / 3);
        reporte.setNumAspirantes(d.numeroAspirantes());
        reporte.setNumNoPreselectos(reporte.getNumAspirantes() - reporte.getNumPreselectos());
        List l = d.consultarPreselectosPorInstitucion();
        List l2 = d.consultarNoPreselectosPorInstitucion();
        int numInst[] = new int[l.size()];
        String nombreInst[] = new String[l.size()];
        Iterator i = l.iterator();
        int n = 0;
        while (i.hasNext()) {
            ArrayList h = (ArrayList) i.next();
            nombreInst[n] = (String) h.get(0);
            numInst[n] = ((Integer) h.get(1)).intValue();
            n++;
        }
        int numInst2[] = new int[l.size()];
        i = l2.iterator();
        n = 0;
        while (i.hasNext()) {
            ArrayList h = (ArrayList) i.next();
            numInst2[n] = ((Integer) h.get(1)).intValue();
            n++;
        }
        reporte.setNumInst(numInst);
        reporte.setNombreInst(nombreInst);
        reporte.setNumInst2(numInst2);
        reporte.setNoPreselectos(d.consultarNoPreselectos());
        String path = Configuracion.getReportesPath() + reporte.getTipo() + reporte.getId() + ".pdf";
        try {
            reporte.generar(path);
            reporte.setPath("");
            request.getSession().setAttribute("reporte", path);
            request.setAttribute("reporte", path);
            return mapping.findForward("success");
        } catch (Exception e) {
            reporte.setMensaje("Problemas con el servidor.");
            return mapping.findForward("failure");
        }
    }
}
